<%@ page import="java.util.*,
                 org.jivesoftware.openfire.XMPPServer,
                 org.jivesoftware.openfire.plugin.LdapSearchPlugin,
                 org.jivesoftware.openfire.user.*,
                 org.jivesoftware.util.*"
%>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

<%  // Get parameters
    boolean save = request.getParameter("save") != null;
    boolean success = request.getParameter("success") != null;
    String searchName = ParamUtils.getParameter(request, "searchName");
    boolean searchEnabled = ParamUtils.getBooleanParameter(request, "searchEnabled");
    int maxResults = ParamUtils.getIntParameter(request, "maxResults", 500);
    boolean violateXEP55 = ParamUtils.getBooleanParameter(request, "violateXEP55");
    boolean useDataForms = ParamUtils.getBooleanParameter(request, "useDataForms");
    boolean andSearch = ParamUtils.getBooleanParameter(request, "andSearch");


    LdapSearchPlugin plugin = (LdapSearchPlugin) XMPPServer.getInstance().getPluginManager().getPlugin("ldapsearch");

    // Handle a save
    Map errors = new HashMap();
    if (save) {
        if (searchName == null || searchName.indexOf('.') >= 0 || searchName.trim().length() < 1) {
            errors.put("searchName", "searchName");
        }
        if (maxResults < 1) {
            errors.put("maxresults","maxresults");
        }

        if (errors.size() == 0) {
            plugin.setServiceEnabled(searchEnabled);
            plugin.setServiceName(searchName.trim());
            plugin.setViolateXEP55(violateXEP55);
            plugin.setMaxResults(maxResults);
            plugin.setUseDataForms(useDataForms);
            plugin.setDefaultAndSearch(andSearch);

            response.sendRedirect("properties.jsp?success=true");
            return;
        }
    }

    searchEnabled = plugin.getServiceEnabled();
    searchName = plugin.getServiceName();
    violateXEP55 = plugin.getViolateXEP55();
    maxResults = plugin.getMaxResults();
    useDataForms = plugin.getUseDataForms();
    searchEnabled = plugin.getServiceEnabled();
    andSearch = plugin.getDefaultAndSearch();

%>

<html>
    <head>
        <title><fmt:message key="properties.form.title" /></title>
        <meta name="pageID" content="ldap-search-properties"/>
    </head>
    <body>

<p>
<fmt:message key="properties.form.directions" />
</p>

<%  if (success) { %>

    <div class="jive-success">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/success-16x16.gif" width="16" height="16" border="0"></td>
        <td class="jive-icon-label">
            <fmt:message key="properties.form.successful_edit" />
        </td></tr>
    </tbody>
    </table>
    </div><br>

<%  } else if (errors.size() > 0) { %>

    <div class="jive-error">
    <table cellpadding="0" cellspacing="0" border="0">
    <tbody>
        <tr><td class="jive-icon"><img src="images/error-16x16.gif" width="16" height="16" border="0"></td>
        <td class="jive-icon-label">
            <fmt:message key="properties.form.error" />
        </td></tr>
    </tbody>
    </table>
    </div><br>

<%  } %>

<form action="properties.jsp?save" method="post">

<div class="jive-contentBoxHeader"><fmt:message key="properties.form.service_enabled" /></div>
<div class="jive-contentBox">
    <p>
    <fmt:message key="properties.form.service_enabled_directions" />
    </p>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
    <tbody>
        <tr>
            <td width="1%">
            <input type="radio" name="searchEnabled" value="true" id="rb01"
             <%= ((searchEnabled) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb01"><b><fmt:message key="properties.form.enabled" /></b></label> - <fmt:message key="properties.form.enabled_details" />
            </td>
        </tr>
        <tr>
            <td width="1%">
            <input type="radio" name="searchEnabled" value="false" id="rb02"
             <%= ((!searchEnabled) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb02"><b><fmt:message key="properties.form.disabled" /></b></label> - <fmt:message key="properties.form.disabled_details" />
            </td>
        </tr>
    </tbody>
    </table>
</div>

<br>

<div class="jive-contentBoxHeader"><fmt:message key="properties.form.default_search_type" /></div>
<div class="jive-contentBox">
    <p>
    <fmt:message key="properties.form.default_search_desc" />
    </p>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
    <tbody>
        <tr>
            <td width="1%">
            <input type="radio" name="andSearch" value="false" id="rb01"
             <%= ((!andSearch) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb01"><b><fmt:message key="properties.form.default_search_or" /></b></label> - <fmt:message key="properties.form.default_search_or_desc" />
            </td>
        </tr>
        <tr>
            <td width="1%">
            <input type="radio" name="andSearch" value="true" id="rb02"
             <%= ((andSearch) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb02"><b><fmt:message key="properties.form.default_search_and" /></b></label> - <fmt:message key="properties.form.default_search_and_desc" />
            </td>
        </tr>
    </tbody>
    </table>
</div>

<br>

<div class="jive-contentBoxHeader"><fmt:message key="properties.form.service_name" /></div>
<div class="jive-contentBox">
    <table cellpadding="3" cellspacing="0" border="0">
    <tr>
        <td class="c1">
           <fmt:message key="properties.form.search_service_name" />:
        </td>
        <td>
        <input type="text" size="30" maxlength="150" name="searchName"  value="<%= (searchName != null ? searchName : "") %>">.<%=XMPPServer.getInstance().getServerInfo().getName() %>

        <%  if (errors.containsKey("searchName")) { %>

            <span class="jive-error-text">t
            <br><fmt:message key="properties.form.search_service_name_details" />
            </span>

        <%  } %>
        </td>
    </tr>
    </table>
</div>

<br>


<div class="jive-contentBoxHeader"><fmt:message key="properties.form.violate_xep55" /></div>
<div class="jive-contentBox">
    <p>
    <fmt:message key="properties.form.violate_xep55_details" />
    </p>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
    <tbody>
        <tr>
            <td width="1%">
            <input type="radio" name="violateXEP55" value="true" id="rb01"
             <%= ((violateXEP55) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb01"><b><fmt:message key="properties.form.enabled" /></b></label>
            </td>
        </tr>
        <tr>
            <td width="1%">
            <input type="radio" name="violateXEP55" value="false" id="rb02"
             <%= ((!violateXEP55) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb02"><b><fmt:message key="properties.form.disabled" /></b></label>
            </td>
        </tr>
    </tbody>
    </table>
</div>

<br>

<br>


<div class="jive-contentBoxHeader"><fmt:message key="properties.form.use_data_forms" /></div>
<div class="jive-contentBox">
    <p>
    <fmt:message key="properties.form.use_data_forms_details" />
    </p>
    <table cellpadding="3" cellspacing="0" border="0" width="100%">
    <tbody>
        <tr>
            <td width="1%">
            <input type="radio" name="useDataForms" value="true" id="rb01"
             <%= ((useDataForms) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb01"><b><fmt:message key="properties.form.enabled" /></b></label>
            </td>
        </tr>
        <tr>
            <td width="1%">
            <input type="radio" name="useDataForms" value="false" id="rb02"
             <%= ((!useDataForms) ? "checked" : "") %>>
            </td>
            <td width="99%">
                <label for="rb02"><b><fmt:message key="properties.form.disabled" /></b></label>
            </td>
        </tr>
    </tbody>
    </table>
</div>

<div class="jive-contentBoxHeader"><fmt:message key="properties.form.max_results" /></div>
<div class="jive-contentBox">
    <p>
    <fmt:message key="properties.form.max_results_details" />
    </p>
    <p>
    <input type="text" size="30" maxlength="150" name="maxResults"  value="<%= (maxResults) %>">
    </p>
    <%  if (errors.containsKey("maxResults")) { %>
        <span class="jive-error-text">
        <br><fmt:message key="properties.form.max_results_error" />
        </span>
    <%  } %>
</div>

<br>

<input type="submit" value="<fmt:message key="properties.form.save_properties" />">
</form>

</body>
</html>